package com.example.prom.ok.y2023.work.ipip;

import com.alibaba.fastjson.JSON;
import com.example.prom.listener.ExcelListener;
import com.example.prom.ok.y2023.work.maxmind.MaxMindIpReaderVO2;
import com.example.prom.strategy.excel.CustomCellStyleStrategy;
import com.example.prom.strategy.excel.CustomColumnWidthStrategy;
import lombok.extern.slf4j.Slf4j;
import net.ipip.ipdb.City;
import net.ipip.ipdb.CityInfo;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static com.alibaba.excel.EasyExcelFactory.read;
import static com.alibaba.excel.EasyExcelFactory.write;

@Slf4j
public class IpInfoHandler {
//    public static void main(String[] args) throws Exception {
//        City db = new City("C:\\Users\\hp\\Desktop\\ok\\work\\ipip-20230803.ipdb\\ipip.ipdb");
//        List<MaxMindIpReaderVO2> MaxMindIpReaderVO2S = readExcel();
//        log.info("去重前的数据总量：{}", MaxMindIpReaderVO2S.size());
//        // ip去重
//        ArrayList<MaxMindIpReaderVO2> removeDup = MaxMindIpReaderVO2S.stream().collect(Collectors.collectingAndThen(
//                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(MaxMindIpReaderVO2::getClient_ip))), ArrayList::new));
//        log.info("去重后的数据总量：{}", removeDup.size());
//        //        System.out.println(maxMindIpReaders.size());
//        List<MaxMindIpReaderVO2> notMatchList = new ArrayList<>();
//        for (MaxMindIpReaderVO2 MaxMindIpReaderVO2 : removeDup) {
////            log.info("maxMindIpReader:{}",JSON.toJSONString(MaxMindIpReaderVO2));
//            String client_ip = MaxMindIpReaderVO2.getClient_ip();
//            CityInfo cn = db.findInfo(client_ip, "CN");
//
//            String isoCode = cn.getCountryCode();
//            System.out.println(isoCode);
//            if (StringUtils.isEmpty(isoCode)) {
//                notMatchList.add(MaxMindIpReaderVO2);
//            }
//        }
////        writeExcel(maxMindIpReaders);
//        writeExcel(notMatchList);
//
//    }

    public static void main(String[] args) throws Exception{
        City db = new City("C:\\Users\\hp\\Desktop\\ok\\work\\ipip-20230803.ipdb\\ipip.ipdb");
        CityInfo cn = db.findInfo("103.25.28.255", "CN");
        System.out.println(JSON.toJSONString(cn));
    }

    public static List<MaxMindIpReaderVO2> readExcel() {
        String fileName = "C:\\Users\\hp\\Desktop\\ok\\data\\register_ip.xlsx";
//        String fileName = "C:\\Users\\hp\\Desktop\\ok\\work\\gatewayBannedIps.xlsx";
        ExcelListener<MaxMindIpReaderVO2> excelListener = new ExcelListener<>();
        read(fileName, MaxMindIpReaderVO2.class, excelListener).sheet().doRead();
        List<MaxMindIpReaderVO2> dataList = excelListener.getDataList();
        return dataList;
    }

    public static void writeExcel(List<MaxMindIpReaderVO2> writerList){
        String writeFileName = "C:\\Users\\hp\\Desktop\\ok\\data\\register_ip1.xlsx";
        write(writeFileName, MaxMindIpReaderVO2.class).sheet().head(MaxMindIpReaderVO2.class)
                .registerWriteHandler(new CustomColumnWidthStrategy())
                .registerWriteHandler(new CustomCellStyleStrategy()).doWrite(writerList);
    }
}
